using Arrowgene.Ddon.GameServer.Characters;
using Arrowgene.Ddon.GameServer.Dump;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared.Entity.PacketStructure;
using Arrowgene.Ddon.Shared.Entity.Structure;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Ddon.Shared.Model.Quest;
using Arrowgene.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Arrowgene.Ddon.GameServer.Handler
{
    public class QuestGetEndContentsGroupHandler : GameRequestPacketHandler<C2SQuestGetEndContentsGroupReq, S2CQuestGetEndContentsGroupRes>
    {
        private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(QuestGetEndContentsGroupHandler));

        public QuestGetEndContentsGroupHandler(DdonGameServer server) : base(server)
        {
        }

        private readonly Dictionary<uint, ContentsType> gContentMapping = new Dictionary<uint, ContentsType>()
        {
            {1, ContentsType.ExtremeMission}, // Seneka (WDT)
            {2, ContentsType.ExtremeMission}, // Issac (WDT)
            {3, ContentsType.ExtremeMission},
            {4, ContentsType.ClanDungeon}, // Clan Dungeon
            {5, ContentsType.ExtremeMission}, 
            {6, ContentsType.ExtremeMission}, // Nayajiku (Lookout Castle)
            {7, ContentsType.ExtremeMission}, // Doris (Mergado)
            {8, ContentsType.ExtremeMission},
            {9, ContentsType.ExtremeMission}, // Alan (WDT)
            {10, ContentsType.ChainDungeon}, // Travers (WDT)
            // May be more...
        };

        public override S2CQuestGetEndContentsGroupRes Handle(GameClient client, C2SQuestGetEndContentsGroupReq request)
        {
            // var pcap0 = new S2CQuestGetEndContentsGroupRes.Serializer().Read(pcap0_data);
            var results = new S2CQuestGetEndContentsGroupRes()
            {
                ContentsType = gContentMapping.ContainsKey(request.GroupId) ? gContentMapping[request.GroupId] : ContentsType.ExtremeMission,
                GroupId = request.GroupId
            };

            var quests = QuestManager.GetQuestsByType(QuestType.ExtremeMission).Where(x => x.Value.MissionParams.Group == request.GroupId).Select(x => x.Value).OrderBy(x => x.QuestId);
            foreach (var quest in quests)
            {
                var entry = quest.ToCDataTimeGainQuestList(0);
                results.TimeGainQuestList.Add(entry);
            }

            return results;
        }

        private static readonly byte[] pcap0_data = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD1, 0x04, 0x03, 0x01, 0x0B, 0x08, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xE7, 0x98, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x21, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x21, 0xE1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x21, 0xE2, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x60, 0x98, 0x00, 0x02, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x71, 0xFA, 0xD1, 0xBC, 0x52, 0x28, 0x68, 0xAD, 0xB2 };
        private static readonly byte[] pcap1_data = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD1, 0x04, 0x03, 0x01, 0x0B, 0x08, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xE7, 0x98, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x01, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x21, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x21, 0xE1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x21, 0xE2, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x26, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x60, 0x98, 0x00, 0x02, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0xAA, 0x00, 0x00, 0x00 };
    }
}
